let l = [("Don",435.);("Nut",75.);("Toto",42.)];;


let compare a b =
  match (a,b) with
      (x,y) when x = y -> 0
    | (x,y) when x < y -> -1
    | _-> 1;;


let rec find name x = 
  match x with
      []-> failwith "Utilisateur non trouve"
    | (x,y)::_ when (String.lowercase x) = (String.lowercase name) -> y
    | _::l -> find name l;;

find "Toto" l;;


let rec add (user, v) lst =
  match lst with
      []-> (user, v)::[]
    | (a,b)::l when (compare (String.lowercase user) (String.lowercase a)) = 0 -> (a,b)::l
    | (a,b)::l->
      if (compare (String.lowercase user) (String.lowercase a)) = 1 then
	(a,b)::add (user, v) l
      else
	(user,v)::(a,b)::l;;

add ("Tonio",4352.) l;;
add ("TotO",4325645.) l;;

(*Suppression des String.lowercase pour conserver le polymorphisme au detriment de la gestion des doublons (majucules/minuscules)*)
let rec delete user lst =
  match lst with
      [] -> []
    | (a,_)::l when user = a -> l
    | (a,b)::l -> (a,b)::delete user l;;

delete "Tonio" l;;

(*Suppression des String.lowercase pour conserver le polymorphisme au detriment de la gestion des doublons (majuscules/minuscules)*)
let rec update user value lst =
  match lst with
      []-> []
    | (a,b)::l when user = a -> (a,value)::l
    | (a,b)::l -> (a,b)::update user value l;;

update "Toto" 42. l;;


let name user newn lst = add (newn,(find user lst)) (delete user lst);;

name "Toto" "Onutela" l;;
name "Nut" "Acdc" l;;


let rec map fct lst =
  match lst with
      []->[]
    | a::l -> (fct a)::map fct l;;


let sales lst reduc = map (function (x,y)-> (x, y*.(100.-.reduc)/.100.)) lst;;

sales l 10.;;


(*Suppression des String.lowercase pour conserver le polymorphisme au detriment de la verification des doublons (majuscules/minuscules)*)
let rec fusion (l1,l2) =
  match (l1,l2) with
      ([],[])->[]
    | ([],(x,y)::l) | ((x,y)::l,[]) -> (x,y)::l
    | ((a,b)::x,(c,d)::y) ->
      if (compare (*String.lowercase *)a (*String.lowercase *)c) = -1 then
	(a,b)::fusion (x,(c,d)::y)
      else if (compare (*String.lowercase *)a (*String.lowercase *)c) = 0 then
	(a,(b+.d))::fusion (x,y)
      else
	(c,d)::fusion ((a,b)::x,y) ;;

let l2 = [("Don",543.56);("Fillipo",674.23);("Tototo", 12.12)];;

fusion (l,l2);;
